package com.disney.dolcn.invitemail.application;
 

import java.util.List;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.disney.dolcn.core.database.DatabaseEngine; 
import com.disney.dolcn.core.init.ServletInitialize;
import com.disney.dolcn.invitemail.bean.Coupon;
import com.disney.dolcn.invitemail.logic.InviteLogic;
import com.disney.dolcn.invitemail.logic.InviteLogicImpl;
import com.disney.dolcn.invitemail.util.ContextUtil;
import com.disney.dolcn.invitemail.util.InviteConstant; 
import com.disney.dolcn.invitemail.util.KeyWordsConstant;
import com.go.teaservlet.ApplicationRequest;
import com.go.teaservlet.ApplicationResponse;

/**
 * 
 * @author hawang
 *
 */
public final class InviteContext {
	private HttpServletRequest req;
	private HttpServletResponse resp;
	private String path="";
	Properties app=null;

	/**
	 * 
	 * @param req
	 * @param resp
	 */
	public InviteContext(ApplicationRequest req, ApplicationResponse resp,String path) {
		this.path=path;
		this.req = req;
		this.resp = resp; 
		app = ServletInitialize.getInstance(req.getSession().getServletContext())
		.getAppProperties();
		String webserviceurl=app.getProperty(InviteConstant.WEBSERVICEURL);
		InviteConstant.WEB_CONFIG=webserviceurl;
	}

	/**
	 * 添加被推荐邮件
	 * 
	 * @param name
	 *            推荐人
	 * @param email
	 *            邮件地址
	 *@param emails
	 *            推荐的邮件地址
	 * @return 0为成功 1为失败 2为邮件格式错误
	 */
	public int addInviteEmail(String name, String email, String emails) {
		InviteLogic logic = new InviteLogicImpl(DatabaseEngine.getInstance()
				.createTemplate(InviteConstant.DB_SECTION_NAME)); 
		
		
		String newname = ContextUtil.repalceContent(name, KeyWordsConstant.getKeyWords(app));
		
		return logic.addInviteUser(newname, email, emails,path);
	}
	/**
	 * 校验关键字
	 * @param info
	 * @return 0 没有包含非法字符 1包含
	 */
	public int findInviteContentKeys(String info) 
	{
		return ContextUtil.findContentKeys(info, KeyWordsConstant.getKeyWords(app));
	}
	 /**
	  * 
	  * @param name
	  * @param email
	  * @return
	  */
	public Coupon getInviteCoupon(String name,String email)
	{
		
		InviteLogic logic = new InviteLogicImpl(DatabaseEngine.getInstance()
				.createTemplate(InviteConstant.DB_SECTION_NAME)); 
		 
		String newname = ContextUtil.repalceContent(name, KeyWordsConstant.getKeyWords(app));
		Coupon c=null;
		try{
			c=logic.getRandomCoupon(newname, email);
		}catch(Exception s){
			s.printStackTrace();
			c=new Coupon();
		}
		return c;
		 
	}

}
